package com.cloudfun.campusshare.repo;

import com.cloudfun.campusshare.common.constant.CashAdvanceStatus;
import com.cloudfun.campusshare.common.constant.PayPlatform;
import com.cloudfun.campusshare.entity.CashAdvanceEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * @author : zhangyongjie
 * @version : 1.0
 * @createTime : 2020/6/2 13:40
 * @description :
 */
public interface CashAdvanceEntityRepo extends JpaRepository<CashAdvanceEntity, String>, JpaSpecificationExecutor<CashAdvanceEntity> {
    @Query(value = "select sum(total_settlement_amount) as historyAmount from cash_advance where status='SUCCESS' and merchant_id= ?1", nativeQuery = true)
    Map computeHistoryAmount(String merchantId);

    @Query(value = "select end_time from cash_advance where status in('SUCCESS','IN_PROGRESSING') and merchant_id=?1 ORDER BY launch_time desc limit 1", nativeQuery = true)
    LocalDate getLastEndDate(String merchantId);

    @Query(value = "select count(id) as count,status as status from cash_advance where merchant_id=?1 group by status", nativeQuery = true)
    List<Map> countGroupByStatus(String merchantId);

    List<CashAdvanceEntity> findByMerchantIdAndStatus(String merchantId, CashAdvanceStatus status);

    CashAdvanceEntity findByMerchantIdAndStatusAndPayPlatform(String merchantId, CashAdvanceStatus status, PayPlatform payPlatform);

    CashAdvanceEntity findByCashAdvanceNo(String cashAdvanceNo);
}
